<?php
namespace app\finance\admin;
use app\system\admin\Admin;
use app\product\model\Supplier as SupplierModel;
use app\finance\model\Account as AccountModel;
use app\finance\model\SystemBalanceLog as SystemBalanceLogModel;
use think\Db;

class SystemBalance extends Admin
{
    protected function initialize()
    {
        parent::initialize();
            $accounts = AccountModel::getSelect(AccountModel::where('id','<>',0)->where('status',1)->all());
            $this->assign('accounts', $accounts);
    }
	public function index()
	{
		if($this->request->isAjax()){
			$where      = $data = [];
            $page       = $this->request->param('page/d', 1);
            $limit      = $this->request->param('limit/d', 15);
            $account_id = $this->request->param('account_id/d', '');
            $cate       = $this->request->param('cate/d', '');
            $type       = $this->request->param('type', 1);
            $where['is_effective'] = 1;
            if(!is_empty($account_id)){
                $where['account_id'] = $account_id;
            }
            if(!is_empty($cate)){
                $where['cate'] = $cate;
            }
            $where['is_effective'] = 1;
            if($type == 2){
                $data['data'] = SystemBalanceLogModel::where($where)
                ->field('*,type as type_text,account_id as account_name')
                // ->page($page)->limit($limit)
                ->order('id desc')->select();
                $data['count'] = SystemBalanceLogModel::where($where)->count();
                $data['code']   = 0;
                foreach ($data['data'] as $key => $value) {
                    if($value['cate'] == 1){
                        $data['data'][$key]['cate_text'] = '入账';
                    }else{
                        $data['data'][$key]['cate_text'] = '出账';
                    }

                }
                return json($data);
            }else{
                $data['data'] = SystemBalanceLogModel::where($where)
                ->field('*,type as type_text,account_id as account_name')
                ->page($page)->limit($limit)->order('id desc')->select();
                $data['count'] = SystemBalanceLogModel::where($where)->count();
                $data['code']   = 0;
                return json($data);
            }

		}
		return $this->fetch();
	}
}
